package com.quanyan.runningriding.web.running;


import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.validation.base.Insert;
import com.quanyan.runningriding.entity.vo.run.MusicVo;
import com.quanyan.runningriding.entity.vo.run.req.BatchSynchroniseMusics;
import com.quanyan.runningriding.entity.vo.run.resp.MusicType;
import com.quanyan.runningriding.entity.vo.run.resp.MyBelovedMusic;
import com.quanyan.runningriding.service.RunMusicService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * RunMusicController
 *
 * @author Tu Chun
 * @date 2016/8/23
 */
@RestController
@RequestMapping(value = "/api/run")
public class RunMusicController {
    @Autowired
    private RunMusicService runMusicService;

    private Logger logger = LoggerFactory.getLogger(RunMusicController.class);

    @RequestMapping(value = "/musicTypeList", method = RequestMethod.POST)
    public APIResponse<List<MusicType>> musicTypeList() {
        APIResponse<List<MusicType>> result = runMusicService.getMusicTypeList();
        return result;
    }

    @RequestMapping(value = "/myBelovedMusicList", method = RequestMethod.POST)
    public APIResponse<PageObj<List<MyBelovedMusic>>> myBelovedMusicList(@RequestBody Map<String, Object> paramMap) {
        String userId = (String) paramMap.get("userId");
        if (userId == null || "".equals(userId)) {
            return APIResponse.returnFail("用户id不能为空");
        }
        Integer pageNum = (Integer) paramMap.get("pageNum");
        if (pageNum == null || pageNum==0) {
            pageNum=1;
        }
        Integer pageSize = (Integer) paramMap.get("pageSize");
        if (pageSize==null||pageSize==0) {
            pageSize=20;
        }
        PageObj<List<MyBelovedMusic>> result =runMusicService.getMyBelovedMusicList(userId,pageNum,pageSize);
        return APIResponse.returnSuccess(result);
    }

    @RequestMapping(value = "/collectionMusic", method = RequestMethod.POST)
    public APIResponse<?> collectionMusic(@RequestBody Map<String, Object> paramMap) {
        String userId = MapUtils.getString(paramMap,"userId");
        Integer musicId = MapUtils.getInteger(paramMap, "musicId");
        Byte operationType = MapUtils.getByte(paramMap,"operationType");

        APIResponse<?> result = runMusicService.collectionMusic(userId, musicId,operationType);
        return APIResponse.returnSuccess(result);
    }
    @RequestMapping(value = "/getMusicByPace", method = RequestMethod.POST)
    public APIResponse<MusicVo> getMusicByPace(@RequestBody Map<String, Object> paramMap) {
        Integer cadence = MapUtils.getInteger(paramMap, "cadence");
        //1-慢跑FM 2-中速FM 3-快跑FM 4-自由FM
        Byte type = MapUtils.getByte(paramMap, "type");

        APIResponse<MusicVo> result = runMusicService.getMusicByPace(type,cadence);
        return result;
    }

    @RequestMapping(value = "/getMusicById", method = RequestMethod.POST)
    public APIResponse<MusicVo> getMusicById(@RequestBody Map<String, Object> paramMap) {
        Integer id = MapUtils.getInteger(paramMap, "id");
        APIResponse<MusicVo> result = runMusicService.getMusicById(id);
        return result;
    }

    @RequestMapping(value = "/batchCollectionMusic", method = RequestMethod.POST)
    public APIResponse<?> batchCollectionMusic(@RequestBody @Validated(Insert.class) BatchSynchroniseMusics batchSynchroniseMusics) {
        APIResponse<?> result = runMusicService.batchCollectionMusic(batchSynchroniseMusics.getMusics());
        return result;
    }

}
